package com.ld.front.webnotice;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ld.common.controller.ProjectBasicController;
import com.ld.common.model.json.ApiResult;
import com.ld.common.webnotice.model.PushMessage;
import com.ld.common.webnotice.model.SocketIOMsgType;
import com.ld.common.webnotice.socket.SocketIOServerUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 网站通知服务
 * 
 * @ClassName WebNoticeController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年07月09日 下午5:35:58
 *
 */
@ConditionalOnProperty(prefix = "shieldsb.socketio", name = "api.enable", havingValue = "true", matchIfMissing = true)
@Api(tags = "网站通知服务") // description 必填 文档显示 标题
@RestController
@RequestMapping("${shieldsb.frontPath}/webnotice")
public class WebNoticeController extends ProjectBasicController<Object> {

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "发送全局消息（公共通知）", notes = "") // value 必填文档显示
    @RequestMapping(value = "/socketioSend", method = { RequestMethod.POST }) // method 必填 否则文档会很冗余
    public ApiResult socketioSend(@ApiParam(name = "message", value = "发送的消息", required = true) @RequestParam String message) {

//        SocketIO socketio = new SocketIO();
//        // 发送公共通知
//        socketio.sendMessage(SocketIOMsgType.TYPE_PUBLIC, message); // 公共通知
        PushMessage pushMessage = new PushMessage();
        pushMessage.setType(SocketIOMsgType.TYPE_PUBLIC);
        pushMessage.setFromUserId("2");
        pushMessage.setFromUserName("超级管理员");
        pushMessage.setMessage(message);
        SocketIOServerUtil.SOCKET_IO_SERVICE.pushMessageToAll(pushMessage);

        return ApiResult.ok("发送成功！");
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "发送全局消息（根据类型）", notes = "") // value 必填文档显示
    @RequestMapping(value = "/socketioSendByType", method = { RequestMethod.POST }) // method 必填 否则文档会很冗余
    public ApiResult socketioSendByType(
            @ApiParam(name = "type", value = "发送的消息", allowableValues = "public_msg,p_welcome_msg,test", required = true) @RequestParam String type,
            @ApiParam(name = "message", value = "发送的消息", required = true) @RequestParam String message) {
//        SocketIO socketio = new SocketIO();

        // 发送公共通知
//        socketio.sendMessage(type, message); // 公共通知
        PushMessage pushMessage = new PushMessage();
        pushMessage.setFromUserId("2");
        pushMessage.setFromUserName("超级管理员");
        pushMessage.setType(type);
        pushMessage.setMessage(message);
//        SocketIO socketio = new SocketIO();
//        socketio.pushMessageToAll(pushMessage);
        SocketIOServerUtil.SOCKET_IO_SERVICE.pushMessageToAll(pushMessage);

        return ApiResult.ok("发送成功！");
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "发送个人消息（根据类型）", notes = "") // value 必填文档显示
    @RequestMapping(value = "/socketioSend2User", method = { RequestMethod.POST }) // method 必填 否则文档会很冗余
    public ApiResult socketioSend2User(@ApiParam(name = "uid", value = "发送到的用户标识", required = true) @RequestParam String uid,
            @ApiParam(name = "type", value = "发送的消息", allowableValues = "public_msg,p_welcome_msg,test", required = true) @RequestParam String type,
            @ApiParam(name = "message", value = "发送的消息", required = true) @RequestParam String message) {

        PushMessage pushMessage = new PushMessage();
        pushMessage.setFromUserId("2");
        pushMessage.setFromUserName("超级管理员");
        pushMessage.setToUserId(uid);
        pushMessage.setType(type);
        pushMessage.setMessage(message);
//        SocketIO socketio = new SocketIO();
//        socketio.pushMessageToUser(pushMessage);
        SocketIOServerUtil.SOCKET_IO_SERVICE.pushMessageToUser(pushMessage);

        return ApiResult.ok("发送成功！");
    }

}
